package com.xuanxu.blogs.framework.swagger.config;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @author XuanXu
 * @version 1.0
 * @date 2024-08-19 13:21
 */
@AutoConfiguration
@EnableSwagger2WebMvc
@EnableConfigurationProperties(SwaggerProperties.class)
@RequiredArgsConstructor
public class SwaggerConfiguration {

    private final SwaggerProperties swaggerProperties;

    @Bean
    public Docket adminConfig() {
        SwaggerProperties.SwaggerEntity adminSwagger = swaggerProperties.getAdminSwagger();
        ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(adminSwagger.isEnable())
                .groupName(adminSwagger.getGroupName())
                .select()
                .apis(RequestHandlerSelectors.basePackage(adminSwagger.getBasePackage()))
                .paths(PathSelectors.any());

        return apiSelectorBuilder.build();
    }

    @Bean
    public Docket appConfig() {
        SwaggerProperties.SwaggerEntity appSwagger = swaggerProperties.getAppSwagger();
        ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(appSwagger.isEnable())
                .groupName(appSwagger.getGroupName())
                .select()
                .apis(RequestHandlerSelectors.basePackage(appSwagger.getBasePackage()))
                .paths(PathSelectors.any());

        return apiSelectorBuilder.build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .version(swaggerProperties.getVersion())
                .build();
    }
}
